<?php

namespace Home\Controller;
use Common\Controller\HomecommController;
class ArticleController extends HomecommController{
    public function index()
    {
        //分类列表
        $where['status'] = 0;//显示
        $where['is_delete'] = 0;
        $cat_list = M('Category')->field('id,title')->where($where)->order('sort ASC')->select();
        $this->assign('cat_list',$cat_list);
        $this->display();
    }

    public function list()
    {
        $category_id = I('post.cat_id');
        $page = I('post.page',1);
        $articleModel = M('Article');
        $limit = 10;
        $start = ($page - 1) * $limit;
        $where['category_id'] = $category_id;
        $where['status'] = 0;//显示
        $where['is_delete'] = 0;
        $list = $articleModel->where($where)->order('create_time DESC')->limit($start,$limit)->select();
        $count = $articleModel->where($where)->count();
        $pageCount = ceil($count/$limit);
        if($count == 0){
            echo json_encode(array('err'=>0,'msg'=>'暂无数据'));
        }else{
            echo json_encode(array('err'=>0,'list'=>$list,'pageCount'=>$pageCount));
        }
    }
    
    /**
     * 详情页
     */
    public function detail()
    {
        $id = I('post.id');
        $articleModel = M('Article');
        $where['id'] = $id;
        $where['status'] = 0;//显示
        $where['is_delete'] = 0;
        $info = $articleModel->where($where)->find();
        if(!info){
            $this->error('数据错误');
        }
        //获取上一篇、下一篇文章  
        $map['cat_id'] = $info['category_id'];  
        $map['id'] = array('neq',$id);  
        $map['create_time'] = array('egt',$data['create_time']);  
        $article_prev = M('Article')->where($map)->order('create_time DESC')->field('id,title')->find();  
         
        $map['create_time'] = array('elt',$data['create_time']);  
        $article_next = M('Article')->where($map)->order('create_time DESC')->field('id,title')->find();  
        
        $this->assign('info',$info);
        $this->assign('article_prev',$article_prev);
        $this->assign('article_next',$article_next);
        $this->display();
    }
}
